//
//  HMWelcomeViewController.swift
//  sinaweibo
//
//  Created by WGP on 16/4/11.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage

class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
//        之前没有用户信息
        setIcon()
        
//        发送网络请求,获取用户信息
        HMUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil
            {
                HMPrint("获取用户信息出错: \(error)")
                return
            }
            
//            获取用户信息成功了
            self.setIcon()
        }
    }
    
    
//    MARK: - 私有方法
    private func setupUI()
    {
//        添加子控件
        view.addSubview(bgImageview)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
//        添加约束
        bgImageview.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            make.width.height.equalTo(86.5)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
private func setIcon()
{
    if let avatar_large = HMUserAccountViewModel.sharedInstance.userAccount?.avatar_large
    {
//        使用sd来下载图片
        let url = NSURL(string: avatar_large)!
//        iconView.sd_setImageWithURL(url, placeholderImage:UIImage(named: "avatar_default_big"))
        self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
    }

}
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewAnimation()
    }
    
//    MARK: - 头像动画
    private func iconViewAnimation()
    {
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height-160))
        }
        UIView.animateWithDuration(1, delay: 0.25, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //通知系统来重新设置布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
//                让label显示出来
                UIView.animateWithDuration(HMDefaultAnimationDuration, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
//                        准备切换控制器
//                        HMPrint("动画完成了,要切换界面了")
                        getApplicationDelegate.switchViewController(HMMainTabBarController())
                })
        }

    }

//    MARK: - 懒加载
//    背景
    private lazy var bgImageview: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
//    头像
    private lazy var iconView: UIImageView =
    {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
//        圆角
        imageView.layer.cornerRadius = 42.5
//        裁剪
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
//    欢迎label
    private lazy var welcomeLabel: UILabel =
    {
        
        let label = UILabel()
        
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(HMWelcomeLabelFontSize)
        label.sizeToFit()
        label.alpha = 0
        
        return label
        
    }()

}








